package com.mowanka.mokeng.ui.fragment.store

import android.os.Bundle
import android.widget.LinearLayout
import androidx.fragment.app.viewModels
import androidx.viewbinding.ViewBinding
import com.blankj.utilcode.util.ScreenUtils
import com.drake.brv.PageRefreshLayout
import com.drake.brv.utils.bindingAdapter
import com.drake.brv.utils.linear
import com.drake.brv.utils.models
import com.drake.brv.utils.setup
import com.mowanka.mokeng.R
import com.mowanka.mokeng.app.base.BaseFragment
import com.mowanka.mokeng.app.util.dp
import com.mowanka.mokeng.app.weight.HomeBannerAdapter
import com.mowanka.mokeng.app.weight.recyclerview.decoration.HorizontalSpacingItemDecoration
import com.mowanka.mokeng.data.model.bean.*
import com.mowanka.mokeng.databinding.FragmentStoreVariableBinding
import com.mowanka.mokeng.databinding.ItemNestedHorizontalRv1Binding
import com.mowanka.mokeng.databinding.ItemNestedHorizontalRvBinding
import com.mowanka.mokeng.databinding.ItemStoreRecommendBinding
import com.mowanka.mokeng.viewmodel.request.RequestStoreViewModel
import com.scwang.smart.refresh.layout.constant.RefreshState
import com.youth.banner.indicator.RoundLinesIndicator
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.parseState

class StoreVariableFragment(private val floorInfo: FloorResponse) : BaseFragment<BaseViewModel, FragmentStoreVariableBinding>() {
    private val requestStoreViewModel by viewModels<RequestStoreViewModel>()

    override fun initView(savedInstanceState: Bundle?) {
        mDatabind.rv.linear().setup {
            addType<PopularSelectionBean>(R.layout.item_nested_horizontal_rv)
            addType<UserPlayingBean>(R.layout.item_nested_horizontal_rv1)
            addType<ConditionHover>(R.layout.layout_condition)
            addType<NewsInfo>(R.layout.item_store_recommend)
            onCreate {
                when (val viewBinding = getBinding<ViewBinding>()) {
                    is ItemNestedHorizontalRvBinding -> {
                        viewBinding.rv.setup {
                            addType<PopularSelection>(R.layout.item_simple_horizontal)
                        }
                    }
                    is ItemNestedHorizontalRv1Binding -> {
                        viewBinding.rv.apply {
                            addItemDecoration(HorizontalSpacingItemDecoration(16.dp, 10.dp))
                        }.setup {
                            addType<UserPlaying>(R.layout.item_simple_horizontal1)
                        }
                    }
                    is ItemStoreRecommendBinding -> {
                        viewBinding.banner.setAdapter(HomeBannerAdapter())
                            .setIndicator(RoundLinesIndicator(requireContext()))
                            .setIntercept(true)
                            .isAutoLoop(false)
                    }
                }
            }
            onBind {
                when (val viewBinding = getBinding<ViewBinding>()) {
                    is ItemNestedHorizontalRvBinding -> {
                        viewBinding.rv.models = getModel<PopularSelectionBean>().list
                    }
                    is ItemNestedHorizontalRv1Binding -> {
                        viewBinding.rv.models = getModel<UserPlayingBean>().list
                    }
                    is ItemStoreRecommendBinding -> {
                        val data = getModel<NewsInfo>()
                        viewBinding.banner.setDatas(getModel<NewsInfo>().picUrls)
                        viewBinding.banner.layoutParams = LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            if (data.coverWidth == 0) 339.dp
                            else (ScreenUtils.getScreenWidth() - 36.dp) * data.coverHeight / data.coverWidth
                        )
                    }
                }
            }
        }
    }

    override fun lazyLoadData() {
        super.lazyLoadData()
        requestStoreViewModel.request(floorInfo)
        mDatabind.page.onRefresh {
            requestStoreViewModel.getProductList(mutableMapOf(), index)
        }.showLoading()
    }

    override fun createObserver() {
        requestStoreViewModel.popResponse.observe(this) { resultState ->
            parseState(resultState, {
                mDatabind.rv.bindingAdapter.apply {
                    addHeader(it, headerCount, true)
                    mDatabind.rv.smoothScrollToPosition(0)
                }
            })
        }
        requestStoreViewModel.userPlayResponse.observe(this) { resultState ->
            parseState(resultState, {
                mDatabind.rv.bindingAdapter.apply {
                    addHeader(it, headerCount, true)
                    mDatabind.rv.smoothScrollToPosition(0)
                }
            })
        }
        requestStoreViewModel.productList.observe(this) { resultState ->
            parseState(resultState, { list ->
                mDatabind.page.let {
                    mDatabind.page.addData(mutableListOf<Any>().apply {
                        //第一页
                        if (it.state == RefreshState.Refreshing || it.index == PageRefreshLayout.startIndex) {
                            add(ConditionHover())
                        }
                        addAll(list)
                    }) {
                        list.size >= 10
                    }
                }
            })
        }
    }
}